﻿
using Catalog.API.Expections;

namespace Catalog.API.Products.GetProductById
{
    public record GetProductByIdResult(Product Product);
    public record GetProductByIdQuery(Guid Id): IQuery<GetProductByIdResult>;

    public class GetProductByIdQueryValidator : AbstractValidator<GetProductByIdQuery>
    {
        public GetProductByIdQueryValidator()
        {
            RuleFor(x => x.Id).NotEmpty().WithMessage("Id cannot be empty");
        }
    }
    public class GetProductByIdQueryHandler(IDocumentSession session) : IQueryHandler<GetProductByIdQuery, GetProductByIdResult>
    {
        public async Task<GetProductByIdResult> Handle(GetProductByIdQuery query, CancellationToken cancellationToken)
        {
            var product=await session.LoadAsync<Product>(query.Id,cancellationToken);
            if(product is null)
            {
               
                string message = $"Product with id {query.Id} not found";
                throw new ProductNotFoundException(message);
            }
            return new GetProductByIdResult(product);
        }
    }
}
